﻿<?if(!defined('sugarEntry'))define('sugarEntry', true);
/*********************************************************************************
module : vng sales marketing
author : hoanghd@vinagame.com.vn
********************************************************************************/
require_once('XTemplate/xtpl.php');
require_once('log4php/LoggerManager.php');
require_once('include/modules.php');
require_once('modules/ZT2Remit/ZT2Remit.php');

global $current_language;
global $app_strings;
global $app_list_strings;
global $theme;
$theme_path = 'themes/' . $theme . '/';
$image_path = $theme_path .'images/';
require_once($theme_path . 'layout_utils.php');
global $mod_strings;



$mode_inpvl=$_POST["mode_inpvl"];
$insert=$_GET["insert"];
$seed = new ZT2Remit_Ext();
$fnc = new FNC();


$pst_mode=$_POST["mode"];
$pst_chk=$_POST["chk"];

//print "<pre>";
//print_r($_SESSION);

// kiem tra quyen access 
if ($seed->ACLAccess('view') && $seed->ACL_Access('PlayingTime')) {
	ob_start();





	$list_form = new XTemplate('modules/ZT2Remit/PlayingTime.html');
	$seed->set_theme($list_form);
	$PN_Error = '';


	$search_type[0]='Tài khoản';
	$search_field[0]='CSMID';
  
   			
    

	$_where="";
	$_vl_search_type=$_POST["search_type"];
	$_vl_keyword=$_POST["keyword"];
    $_vl_pcs = $_POST["pcs"];
    $_vl_timeplaying = $_POST["timeplaying"];

  
	if ($_vl_keyword!='' && $_vl_search_type!=''){
			$_where.=" and ".$search_field[$_vl_search_type]." like ''%".$seed->anti($_vl_keyword)."%'' ";
            echo "aaaaaaaaaaaaaaaaaaaaaaaaa";
           ;
            
	}
    
     //
     
    if ($_vl_pcs != '')
    {
        $_where .= " and PCs >= ".$seed->anti($_vl_pcs);        
          
    }
   // else 
    if ($_vl_timeplaying != '')
    {
        $_playingTimeGiay = $seed->anti($_vl_timeplaying)*60;
        echo $_playingTimePhut;     
         $_where .= " and PlayingTime >= ".$seed->anti($_playingTimeGiay);
       
        
        
    }
	

	$gridview='';
    $DBList = $seed->setList($fnc,$_where,$_vl_from_date,$_vl_to_date,$locationId,$_pst_statusId,$list_form);   
   
	$gridview.= $seed->_AddPageHeader();
	$gridview.= $seed->PageHeader();
	$exportGrid = $seed->setHeader();
	$exportGrid.= $DBList[0];
	$gridview.= $exportGrid;		
	$gridview.=$seed->setFooter();
	$gridview.=$seed->setPaging();

	
	$title='Danh sách Playing time';
	if($_POST['mode']=='EXPORT' && $seed->ACL_Page(1,'List'))
	{			
		$seed->ExportToExcel($seed->fnStrConvert($title),$exportGrid);							
		die();
	}



  // them 2 textbox
 

	$arr_attr_pcs = array('style'=>'');
	$inp_pcs = $fnc->addInput('text','pcs',$pcs,$arr_attr_pcs,$list_form);    
   

  
    $arr_attr_timeplaying= array('style'=>'');
	$inp_timeplaying  = $fnc->addInput('text','timeplaying',$timeplaying,$arr_attr_timeplaying,$list_form);    
   
   
    //
    

	$attr_search_type = array("style"=>'','title'=>'Tìm kiếm theo');
	$search_type = $fnc->addSelectList('search_type',$search_type,'--- Tìm kiếm theo ---',$attr_search_type,$list_form);	


	$arr_attr_keyword = array('style'=>'width:250px;');
	$inp_keyword = $fnc->addInput('text','keyword',$keyword,$arr_attr_keyword,$list_form);
  
 
    
	if ($seed->ACL_Page(1,'List')){	
		$arr_attr_btnexport = array('style'=>'','onclick'=>'sbm_form(2)');
		$inp_btnexport = $fnc->addInput2('button','btnexport',$btnexport,$arr_attr_btnexport,$list_form,'Xuất Excel'); 
	}
    

	/*----------------------*/
	$list_form->assign('tabs'	,  	$seed->set_tabs());
	/*----------------------*/	
    $_totalPCstemp = number_format($seed->_totalPCs,0,'','.');
    //$_totalPlayingTimetemp = number_format($seed->_totalPlayingTime,0,'','.');
    $_totalPMstemp =  number_format($seed->_totalRecord,0,'','.');
    $list_form->assign('PMsTotal',$_totalPMstemp);
    $list_form->assign('PCsTotal',$_totalPCstemp);   
    $list_form->assign('PlayingTimeTotal',$seed->_totalPlayingTime/*$_totalPlayingTimetemp*/);
	$list_form->assign('MODULE'	,  	$seed->object_name);
	$list_form->assign('PN_Error',	$PN_Error);
	$list_form->assign('P_INFO',	$P_INFO);
	$list_form->assign('gridview'	,  	$gridview);
	$list_form->assign('title'	,  	$title);
	$list_form->assign('page_name'	,  	$sugar_config['submenu_name'][$seed->object_name]);

	$list_form->parse('main');
	$list_form->out('main');
	
	ob_end_flush();
} else {
	ACLController::displayNoAccess();
	sugar_die('');
}

class ZT2Remit_Ext  extends ZT2Remit
{
    
       
        
                    
	//======================//
	function _AddPageHeader(){
		$_PageHeader=' <br>';	
		return 	$_PageHeader;	
	}
    


	function setHeader(){		

		if ($_POST['mode']=='EXPORT' || !$this->ACL_Page(1,'List')){
			$ArrHeader = array('STT'					
			
			,'Tài khoản'			
			,'Số lượng PCs'
			,'Playing time(phút)'				
					
			);
		}
		else{
			$ArrHeader = array(						
		      'STT'
			,'Tài khoản'			
			,'Số lượng PCs'
			,'Playing time(phút)'				
					
			);
			
		}
		$SortHeader = array(0,0,0,0,0,0,0,0);
		//$SortImage[$_POST['_SortOrderBy']]='css_right ui-icon ui-icon-carat-2-n-s';
		if ($_POST['_OrderDirection']=='desc'){
			$SortImage[$_POST['_SortOrderBy']]='css_right ui-icon ui-icon-triangle-1-s';
		}else if ($_POST['_OrderDirection']=='asc'){
			$SortImage[$_POST['_SortOrderBy']]='css_right ui-icon ui-icon-triangle-1-n';
		}
		
		
		$_Header='';
		$_Header.='<div class="hastable"><div id="aj_result"><table   width="100%"  cellspacing="0"  cellpadding="0" border=0>';
		$_Header.='<thead><tr>';
		
		for ($j=0;$j<count($ArrHeader);$j++)
		{
		
			if ($SortHeader[$j])
			{
				if ($SortImage[$j]==''){$SortImage[$j]='css_right ui-icon ui-icon-carat-2-n-s';}
				
				$_Header.='<th class="ui-state-default t_cap" valign=top>
				<a href=javascript:fnOrder('.$j.') >'.$ArrHeader[$j].'
					<span class="'.$SortImage[$j].'"></span>
				</a></th>';
			}
			else
				$_Header.='<th class="ui-state-default t_cap">'.$ArrHeader[$j].'</th>';
		}
		$_Header.='</tr></thead>';
		return $_Header;
	}
    
    //function setList($fnc,$_Where,$_vl_from_date,$list_form){
    function setList($fnc,$_Where,$_vl_from_date,$_vl_to_date,$locationId,$_pst_statusId,$list_form){	
		$result = NULL;	
		$PageSize=10;		
		$this->PageIndex = isset($_POST["page"]) ? $_POST["page"] : 1;		
		if ($_POST['ddlPageSize']!=''){$PageSize=$_POST['ddlPageSize'];}
		$this->PageSize = $PageSize;
		$_FlgExport=false;
		if($_POST['mode']=='EXPORT'){$PageSize=0;$_FlgExport=true;}

		$_Table = '';
		if ($_FlgExport)
		{	$_Field = array('configName','gamecode','from_date','to_date');	
		}else{
			$_Field = array('configName','gamecode','from_date','to_date');	
		}
		
		$_strField = implode(',', $_Field);
		if ($filter!='')	
		{
			$filter_arr = explode('|',$filter);
		}
			
	
		// ===== order =============/
		$_SortOrderBy=$_POST["_SortOrderBy"];
		$_OrderDirection=$_POST["_OrderDirection"];
		
		if (trim($_SortOrderBy)=='' || $_FlgExport)
		{
			$_OrderBy = 'CSMID';
			$_OrderDirection = 'asc';	
		}
		else
		{
			$_OrderBy = $_Field[$_SortOrderBy-1];			
		}
		// ===== order =============/
		
		try
		{	

	
			$_dblist='';			
			$i=0;
			
		
			if ($_POST['mode_inpvl']=='EXPORT')			
			 $sSQL=" usp_ZT2_Remit_be_Report_List 1,10,' and 1=1 $_Where ','".$_OrderBy."','".$_OrderDirection."','*'";			
		
            else
					
			$sSQL=" usp_ZT2_Remit_be_Report_List ".$this->PageIndex.",".$PageSize.",' and 1=1 $_Where  ','".$_OrderBy."','".$_OrderDirection."','*'";			
			
      
            
			$this->ws_debug($sSQL);
				
				$result = $this->db->query($sSQL, true, "Query failed");					
				
				
			
				
				while ($aR = $this->db->fetchByAssoc($result))
				{
					
					if ($i==0){
						$this->TotalRecord = $fnc->isNull($aR['TotalRecord'],0);						
						$this->TotalPage = intval($this->TotalRecord/$PageSize + ($this->TotalRecord%$PageSize > 0?1:0));					
						$this->_totalRecord = $fnc->isNull($aR['TotalRecord'],0);
                        $this->_totalPCs =  $fnc->isNull($aR['TotalPCs'],0);
                        $temp =  $fnc->isNull($aR['TotalPlayingTime'],0);                                                
                        $this->_totalPlayingTime = number_format(ceil($temp / 60),0,'','.');
                       
                        
					}					
				
					$STT = ($i+($this->PageIndex-1)*$PageSize)+1;                   
                    $_acount = $aR['CSMID'];
                   	$_PCs = $aR['PCs']; 
                    $_timeSecond = $aR['PlayingTime'];
					$_playingTime = ceil($aR['PlayingTime']/60);
                                    	
					
					$_dblist.='<tr>';
							
							if ($_POST['mode']=='EXPORT' || !$this->ACL_Page(1,'PlayingTime')){
							//	$_dblist.='<td  style="text-align:center" width=50>'.$STT.'</td>';							
							}else{

								//$arr_attr_keyword = array('style'=>'');
							//	$inp_del = '<input type=checkbox value="'.$Id.'" name="chk[]" id="chk_'.$Id.'">';
							//	$_dblist.='<td  style="text-align:center" width=50>'.$inp_del.'</td>';							
							}
                            
							$_dblist.='<td  style="text-align:left">'.$STT.'</td>';
                            $_dblist.='<td  style="text-align:left">'.$_acount.'</td>';
                            $_dblist.='<td  style="text-align:left">'.$_PCs.'</td>';
                            $_dblist.='<td  style="text-align:left">'.$_playingTime.'</td>';
                           
                         
                         // hiden detail in export
                          	if ($_POST['mode']!='EXPORT' || !$this->ACL_Page(1,'PlayingTime'))
                              {
                         	 
                                 if ($this->ACL_Page(1,'PlayingTime')){
                                     $_dblist.='<td  style=" text-align:left"><a  style="color:#00F;" href="javascript:GetDetail(\''.$_acount.'\','.$_PCs.','.$_playingTime.')">Chi tiết</a></td>';
                                 }else{
                                     $_dblist.='<td  style="text-align:left">#</td>';                            
                                    
                                 }
                         } 
                          
																												
					$_dblist.='</tr>';			
				
					
					$i++;
				}				
			
			
			if ($i==0){
					if ($this->TotalRecord==0)
						{
							$_dblist.='<tr><td colspan=100>Không tìm thấy thông tin</td></tr>';	
						}	
			}else{	}
			
				
		}
		catch (PDOException $ex)
		{}			
		
		$list[0] = $_dblist;

			
		return $list;			
		
	}	
    
    
	function fnc_get_locationId($Id){
			$sSQL="exec usp_vng_location 2,".$Id." ,0,0 ";
			$result = $this->db->query($sSQL, true, "Query failed");	
			$aR = $this->db->fetchByAssoc($result);
			return '0'.$aR['id'];	
	}





							  
								  




}


?>